<?php

namespace App\Http\Controllers\Home;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use App\Http\Requests\UserRegistRequest;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
class LoginController extends Controller
{
	public function getRegist()
	{
		//显示注册页面
		return view('home.login.regist');
	}

	//处理ajax发送的手机号
	public function getCheck(Request $request)
	{
		
		$phone=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('phone',$phone['phone'])->first();
		// dd($res);
		if(empty($res)){
			echo 0;
		}else{
			echo 1;
		}
		
	}

	//手机 验证码
	public function Vcode()
	{
		ob_clean();//清除输出缓存区的内容
	    //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 34, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

	}

	

	//处理ajax发送的验证码
	public function getCode(Request $request)
	{
		$code=$request->input('code');
		//检测验证码是否正确
		if($code == session('Vcode')){
			echo 1;
		}else{
			echo 0;
		}
	}

	//处理ajax发送的手机号，做短信验证
	public function Send($phone)
	{
		//初始化必填
		$options['accountsid']='4dd17dbd5bbe1a676de013843a489e88';
		$options['token']='7d89c27f795ed673743099c4ac2ac8c7';
		//初始化 $options必填
		$ucpass = new \Ucpaas($options);
		//开发者账号信息查询默认为json或xml
		// echo $ucpass->getDevinfo('json');

		//短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
		$appId = "a510188d0a4d448abbf5448bf307fec1";
		$to = $phone;
		$templateId = "35397";//短信模板的id
		$rand=rand(11111,99999);
		session(['messagecode' => $rand]);

		$param="虎扑论坛,".$rand.",3";
		echo $ucpass->templateSMS($appId,$to,$templateId,$param);
	}

	//调用短信发送方法，发送短信
	public function getMessage(Request $request)
	{
		$phone=$request->input('phone');
		// dd($phone['phone']);
		$res=$this->Send($phone);
		if ($res) {
			echo 1;
		}else{
			echo 0;
		}
	}

	//验证手机验证码
	public function getYanzheng(Request $request)
	{
		if ($request->input('mcode')  == session('messagecode')) {
			echo 1;
		}else{
			echo 0;
		} 
	}


	//处理注册数据
	public function postDoregist(Request $request)
	{
		
		// dd($request->all());
		//检测验证码是否正确
		// if($request->input('code') != session('Vcode')){
		// 	return back()->with('error','验证码不正确');
		// }
		// if($request->input('mcode') != session('messagecode')){
		// 	return back()->with('error','短信验证码不正确');
		// }

		//处理数据
		$data=$request->only(['phone','password']);
		$data['username']='hp_'.rand(11111111,99999999);
		$data['regtime']=time();
		$data['password']=Hash::make($data['password']);
		$data['token']=str_random(50);
		//添加数据 并获取id
		$id=DB::table('users')->insertGetId($data);
		if($id){
			//注册成功
			// session(['uid'=>$id]);
			// return view('home.login.success');
			return view('home.login.login');
		}else{
			//注册失败
			return back()->with('error','注册失败');
		}
	}

	public function getLogin()
	{
		//显示登录页面
		return view('home.login.login');
	}

	//判断ajax发送的用户是否存在
	public function getExist(Request $request)
	{
		$value=$request->input('username');
		$res1=DB::table('users')->where('username',$value)->first();
		$res2=DB::table('users')->where('phone',$value)->first();
		$res3=DB::table('users')->where('email',$value)->first();
		if(!empty($res1 || $res2 || $res3)){
			echo 1;
		}else{
			echo 0;
		}
		
	}

	//发送ajax判断密码是否正确
	public function postPass(Request $request)
	{

		$pass=$request->input('password');
		$value=$request->input('username');
		$res1=DB::table('users')->where('username',$value)->first();
		$res2=DB::table('users')->where('phone',$value)->first();
		$res3=DB::table('users')->where('email',$value)->first();
		if(!empty($res1)){
			$res=$res1->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
				session(['uid'=>$res1->uid,'pic'=>$res1->pic,'username'=>$res1->username]);				
				echo 1;
			}else{
				echo 0;
			}
		}else if(!empty($res2)){
			$res=$res2->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
				//登录成功转到首页
				//将用户信息存入session
				// session()->forget('uid');
				session(['uid'=>$res2->uid,'pic'=>$res2->pic,'username'=>$res2->username]);
								
				echo 1;
			}else{
				echo 0;
			}
		}else if(!empty($res3)){
			$res=$res3->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
				//登录成功转到首页
				//将用户信息存入session
				// session()->forget('uid');
				session(['uid'=>$res3->id,'pic'=>$res3->pic,'username'=>$res3->username]);				
				echo 1;
			}else{
				echo 0;
			}
		}
		else{
			echo 0;
		}		
	}

	//处理登录数据
	public function postDologin(Request $request)
	{
		$post=$request->only('username','password');
		$pass=$post['password'];
		$value=$post['username'];
		$res1=DB::table('users')->where('username',$value)->first();
		$res2=DB::table('users')->where('phone',$value)->first();
		$res3=DB::table('users')->where('email',$value)->first();
		if(!empty($res1) && !empty(session('uid'))){
			$res=$res1->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
				return redirect('/');
				
			}else{
				return back()->with('error','登录失败');
			}
		}else if(!empty($res2) && !empty(session('uid'))){
			$res=$res2->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
			
				return redirect('/');
			}else{
				return back()->with('error','登录失败');
			}
		}else if(!empty($res3) && !empty(session('uid'))){
			$res=$res3->password;
			//检查密码是否正确
			if(Hash::check($pass,$res))
			{
				
				return redirect('/');
			}else{
				return back()->with('error','登录失败');
			}
		}
		else{
			return back()->with('error','登录失败');
		}

	}

	

	


	/*
		手机找回密码
	*/
	//显示找回页面
	public function getFind()
	{
		//显示找回页面
		return view('home.login.find');
	}

	//处理重置的密码
	public function postResetpass(Request $request)
	{
		
		//检测验证码是否正确
		// if($request->input('code') != session('Vcode')){
		// 	return back()->with('error','验证码不正确');
		// }
		//处理数据
		$data=$request->only(['password','phone']);
		$data['password']=Hash::make($data['password']);
		$data['token']=str_random(50);
		//添加数据 并获取id
		$res=DB::table('users')->where('phone',$data['phone'])->update($data);
		if($res==1){
			//重置成功
			return view('home.login.login');

			// echo $res;
		}else{
			//重置失败
			return back();
		}
	}

	/*
		邮箱找回密码
	*/
	//邮箱 验证码
	public function Ecode()
	{
		ob_clean();//清除输出缓存区的内容
	    //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 34, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Ecode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

	}

	//邮件 处理ajax发送的验证码
	public function getEmcode(Request $request)
	{
		$code=$request->input('code');
		// dd($code == session('Ecode'));
		//检测验证码是否正确
		if($code == session('Ecode')){
			echo 1;
		}else{
			echo 0;
		}
	}

	

	//处理ajax发送的邮箱是否存在
	public function getEmail(Request $request)
	{
		
		$email=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('email',$email['email'])->first();
		// dd($res);
		if(empty($res)){
			echo 0;
		}else{
			echo 1;
		}
		
	}

	//发送邮箱验证码
	public function getEmailcode(Request $request)
	{
		$email=$request->input('email');
		
		$rand=rand(11111,99999);
		session(['emessagecode' => $rand]);
		session(['email' => $email]);
		

		Mail::raw('虎扑论坛向您发送的验证码为:'.$rand, function($message){
	        global $email;
	        //设置邮件标题
	        // dd(session('email'));
	        $message->subject('验证邮件');
	        //发送给谁
	        $message->to(session('email'));
	        
    	});

		// // dd($request->input('email'));
		// $email=$request->input('email');
		// $res=$this->sendEmail($email);
		// if($res){
		// 	echo 1;
		// }else{
		// 	echo 0;
		// }
	}

	//发送邮件
	// public function sendEmail($email)
	// {
	// 	// dd($email);
	// 	$rand=rand(11111,99999);
	// 	session(['emessagecode' => $rand]);
	// 	Mail::raw('虎扑论坛向您发送的验证码为:'.$rand, function($message){
 //        //设置邮件标题
 //        $message->subject('验证邮件');
 //        //发送给谁
 //        $message->to('840341562@qq.com');
        
 //    	});
	// }

	//比对发送的邮箱验证码与输入的是否一致
	public function getYouxiang(Request $request)
	{

		if ($request->input('message')  == session('emessagecode')) {
			echo 1;
		}else{
			echo 0;
		} 
	}


	public function postUpdate(Request $request)
	{
		dd($request->all());
	}



}
